import Modal from "antd/es/modal/Modal";
import {useEffect, useState} from "react";
import {Form, Input, message, Switch} from "antd";
import TextArea from "antd/es/input/TextArea";
import http from "../../../utils/http";

const EditModal = (props) => {
    const [form] = Form.useForm();
    const [data,setData] = useState({});
    const [isModalVisible,setIsModalVisible] = useState(false);
    let formData = props.data;
    useEffect(()=>{
        setIsModalVisible(props.isShow);
        setData(props.data);
    },[props]);
    form.setFieldsValue(formData);
    const handleCancel = () => {
        setIsModalVisible(false);
        setData(props.data);
        props.close();
    }
    const handleOk = () => {
        console.log(formData)
        http({
            url:'api/user/updateLevel',
            method:'post',
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            params: formData
        }).then(res=>{
            console.log(res)
            if (res.code == 200){
                message.success('修改成功');
            }
        })
        setIsModalVisible(false);
        props.close();
    }
    return (
        <Modal title="修改" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
            <Form
                name="basic"
                labelCol={{span: 8}}
                wrapperCol={{span: 16}}
                form={form}
                initialValues={formData}
                autoComplete="off"
            >
                <Form.Item
                    label="消费金额"
                    name="consumeRule"
                    rules={[
                        {
                            required: true,
                            message: '请输入等级消费金额',
                        },
                    ]}
                >
                    <Input value={formData.consumeRule} onChange={(ev)=>{formData.consumeRule = ev.target.value}} placeholder={'请输入等级消费金额门槛'} />
                </Form.Item>
                <Form.Item
                    label="对应折扣"
                    name="discount"
                    rules={[
                        {
                            required: true,
                            message: '请输入折扣(0-1)'
                        }
                    ]}
                >
                    <Input value={formData.discount} onChange={(ev)=>{formData.discount = ev.target.value}} type={'digit'} placeholder={'请输入折扣（0-1）'} />
                </Form.Item>
                <Form.Item
                    label="名称"
                    name="name"
                    rules={[
                        {
                            required: true,
                            message: '请输入名称'
                        }
                    ]}
                >
                    <Input value={formData.name} onChange={(ev)=>{formData.name = ev.target.value}} placeholder={'请输入名称'} />
                </Form.Item>
                <Form.Item
                    label="购买商品数量"
                    name="quantityRule"
                    rules={[
                        {
                            required: true,
                            message: '请输入购买商品数量门槛'
                        }
                    ]}
                >
                    <Input value={formData.quantityRule} onChange={(ev)=>{formData.quantityRule = ev.target.value}} placeholder={'请输入购买商品数量门槛'} />
                </Form.Item>
                <Form.Item
                    label="注册时间"
                    name="regTimeRule"
                    rules={[
                        {
                            required: true,
                            message: '请输入注册时间'
                        }
                    ]}
                >
                    <Input value={formData.regTimeRule} onChange={(ev)=>{formData.regTimeRule = ev.target.value}} placeholder={'请输入注册时间'} />
                </Form.Item>
                <Form.Item
                    label="等级简述"
                    name="overview"
                >
                    <TextArea
                        value={formData.overview}
                        onChange={(ev)=>{formData.overview = ev.target.value}}
                        placeholder='请输入等级简述'
                        autoSize={{ minRows: 3, maxRows: 5 }}
                    />
                </Form.Item>
                <Form.Item
                    label="等级简述"
                    name="status"
                >
                    <Switch defaultChecked={formData.status} onChange={(val)=>{formData.status = val}} />
                </Form.Item>
            </Form>
        </Modal>)
}

export default EditModal;